﻿using ILRuntime.Mono.Cecil;
using Smart.Model.UI;
using System;
using System.Collections;
using UnityEngine;

namespace Smart.Model
{
    public class Entry
    {
        public static Entry Instance { get; set; }

        GameObject gameObject;
        Transform transform;
        public GameObject NGUIRoot { get; private set; }
        public Transform NGUIRootTransform { get; private set; }

        public Entry(GameObject gameObject,GameObject uiroot)
        {
            NGUIRoot = uiroot;
            NGUIRootTransform = NGUIRoot.transform;
            this.gameObject = gameObject;
            transform = this.gameObject.transform;
            EventSystem = new EventSystem();
            EventSystem.Init(EventSystem.MODEL, GetType().Assembly.GetTypes());
            gameObject.name = "游戏模型层";
            ObjectPool = AddViewComponent<ObjectPool,Transform>(transform);
            Scene = AddViewComponent<SceneManager, Transform>(transform);
            ResourceComponent = Scene.AddComponent<ResourceComponent>();
            TableComponent = Scene.AddComponent<TableComponent>();
            //FrameComponent = Scene.AddComponent<FrameComponent>();
        }

        public void Dispose()
        {
            TableComponent = null;
            ResourceComponent = null;
            Scene.Dispose();
            Scene = null;
            ObjectPool.Dispose();
            ObjectPool = null;
            EventSystem.Dispose();
            EventSystem = null;
            this.gameObject = null;
            NGUIRootTransform = null;
            NGUIRoot = null;
        }

        public T AddViewComponent<T,A>(A a) where T : ComponentView, new()
        {
            T component = new T();
            EventSystem.AddComponent(component);
            EventSystem.Awake(component,EventSystem, a);
            return component;
        }

        public SceneManager Scene;
        public ObjectPool ObjectPool;
        public EventSystem EventSystem;
        public ResourceComponent ResourceComponent;
        public TableComponent TableComponent;
        //public FrameComponent FrameComponent;
    }
}